package com.ledim.adapter.base;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.LogUtil;
import java.util.ArrayList;
import java.util.List;

public abstract class LedimBaseListAdapter<D> extends ArrayAdapter<D>
        implements AbsListView.OnScrollListener {

    private List<D> mInfos;

    protected LayoutInflater mInflater;

    protected Context context;

    protected List<AbsListView.OnScrollListener> onScrollListeners;

    public LedimBaseListAdapter(Context context) {
        super(context, android.R.layout.simple_list_item_2);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
    }

    public void setOnScrollListener(AbsListView.OnScrollListener listener) {
        if (onScrollListeners == null) {
            onScrollListeners = new ArrayList<AbsListView.OnScrollListener>();
        }
        onScrollListeners.add(listener);
    }

    public int getCount() {
        return mInfos == null ? 0 : mInfos.size();
    }

    public D getItem(int position) {
        return mInfos == null ? null : mInfos.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public abstract View getView(int position, View convertView, ViewGroup parent);

    /**
     * 可以在一开始setDatas之后操作infos直接notify即可，无需再次调用setDatas，因为是同一个list对象
     *
     * @param infos
     */
    public void setDatas(List<D> infos) {
        mInfos = infos;
        notifyDataSetChanged();
    }

    public void setMoreDatas(List<D> infos) {
        if (mInfos == null) {
            mInfos = new ArrayList<D>();
        }
        mInfos.addAll(infos);
        notifyDataSetChanged();
    }

    public void setInfos(List<D> infos) {
        mInfos = infos;
    }

    public List<D> getDatas() {
        return mInfos;
    }

    public void hideLastPositionView(int position, View v) {
        v.setVisibility(position == getCount() - 1 ? View.GONE : View.VISIBLE);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (onScrollListeners != null) {
            for (AbsListView.OnScrollListener listener : onScrollListeners) {
                listener.onScrollStateChanged(view, scrollState);
            }
        }
        switch (scrollState) {

            case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                FrescoUtil.pause();//处于滚动状态锁定图片加载线程
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                //设置要加载图片的起始位置和结束位置
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        FrescoUtil.resume();//解除锁定
                    }
                }, 1000);
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                FrescoUtil.pause();
                break;
            default:
                FrescoUtil.pause();
                break;

        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                         int totalItemCount) {
        if (onScrollListeners != null) {
            for (AbsListView.OnScrollListener listener : onScrollListeners) {
                listener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
            }
        }
    }
}
